<?php
!defined('IN_FENGTOPsso') && exit('Access Denied');

require_once FENGTOPsso_DIR.'config/global.ini.php';

class FENGTOPsso {
	
	private $action;
	
	public function FENGTOPsso() {
		$this->setup();
		$this->InitDB();
		$this->InitSession();
	}
	private function setup(){
		$this->action = @$_REQUEST['action'];
	}
	public function ssoRun() {
		switch ($this->action){
			case 'register':
				$this->ssoRegister();
				break;
			case 'login':
				$this->ssoLogIn();
				break;
			case 'logOut':
				$this->ssoLogOut();
				break;
			case 'checkLog':
				$this->checkLog();
				break;
			case 'registerActive':
				$this->registerActive();
				break;
		}
	}
	public function InitDB() {
		require_once FENGTOPsso_DIR.'database/dbmysql.php';
		$GLOBALS['FENGTOPssoDB'] = new DB($GLOBALS['_FENGTOPsso']['db']['1']['dbhost'], $GLOBALS['_FENGTOPsso']['db']['1']['dbuser'], $GLOBALS['_FENGTOPsso']['db']['1']['dbpw'], $GLOBALS['_FENGTOPsso']['db']['1']['dbname']);
	}
	
	public function ssoRegister() {
		require_once FENGTOPsso_DIR.'class/register.class.php';
		$register = new Register();
		$register->checkIn();
		$register->writeInSQL();
//echo $register->msg;
		$GLOBALS['fetcher'] .= "&msg=".$register->msg;
	}
	
	public function ssoLogIn() {
		require_once FENGTOPsso_DIR.'class/login.class.php';
		$login = new LogIn();
		if($login->checkIn() == 1){
			$Result = array("Result" => 1);
			echo json_encode($Result);;
			exit;
		}
		else { 
			$tmp = $login->selectSql();
			if($tmp == -1){
				$Result = array("Result" => -1);
			}else{
				$Result = array("Result" => 0);
			}
			echo json_encode($Result);
			exit;
		}
		$login->setFetcher();
	}
	
	private function InitSession() {
		$this->lifeTime    = $GLOBALS['_FENGTOPsso']['config']['sessiontime'];
		$this->sessionPath = $GLOBALS['_FENGTOPsso']['config']['sessionpath'];
		session_save_path($this->sessionPath);
		session_set_cookie_params($this->lifeTime); 
		
		if(isset($_COOKIE['PHPSESSID'])){
			session_id($_COOKIE['PHPSESSID']);
			session_start();
			@$_SESSION['UserTrack'][4] = $_SESSION['UserTrack'][3];
			@$_SESSION['UserTrack'][3] = $_SESSION['UserTrack'][2];
			@$_SESSION['UserTrack'][2] = $_SESSION['UserTrack'][1];
			@$_SESSION['UserTrack'][1] = $_SESSION['UserTrack'][0];
			$_SESSION['UserTrack'][0] = $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
		}
		else {
			session_start();
			setcookie("PHPSESSID", session_id(), time()+$GLOBALS['_FENGTOP']['config']['sessiontime'], "/", $GLOBALS['_FENGTOP']['web']['url']); 
			@$_SESSION['UserTrack'][4] = $_SESSION['UserTrack'][3];
			@$_SESSION['UserTrack'][3] = $_SESSION['UserTrack'][2];
			@$_SESSION['UserTrack'][2] = $_SESSION['UserTrack'][1];
			@$_SESSION['UserTrack'][1] = $_SESSION['UserTrack'][0];
			$_SESSION['UserTrack'][0] = $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];
		}
	}
	
	public function ssoLogOut() {
		require_once FENGTOPsso_DIR.'class/logout.class.php';
		$logout = new LogOut();
		$logout->clearTicket();
		$logout->delOnLine();
	}
	
	public function checkLog() {
		require_once FENGTOPsso_DIR.'class/checklog.class.php';
		$checklog = new CheckLog();
		$checklog->checkUserLogin();
		$checklog->coutResult();
	}
	
	private function registerActive(){
		require_once FENGTOPsso_DIR.'class/registerActive.class.php';
		$registerActive = new RegisterActive();
		$registerActive->registerActiveRun();
		$GLOBALS['fetcher'] = 'http://fengtop.net/index.php';
	}
}

?>